package com.fourforfo.fourmall.product.service.impl;

import com.fourforfo.fourmall.product.entity.AttrEntity;
import com.fourforfo.fourmall.product.service.AttrService;
import com.fourforfo.fourmall.product.vo.spu.BaseAttrs;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fourforfo.common.utils.PageUtils;
import com.fourforfo.common.utils.Query;

import com.fourforfo.fourmall.product.dao.ProductAttrValueDao;
import com.fourforfo.fourmall.product.entity.ProductAttrValueEntity;
import com.fourforfo.fourmall.product.service.ProductAttrValueService;


@Service("productAttrValueService")
public class ProductAttrValueServiceImpl extends ServiceImpl<ProductAttrValueDao, ProductAttrValueEntity> implements ProductAttrValueService {

    @Autowired
    AttrService attrService;

    /**
     * 修改商品规格,根据attrId和spuId修改attrName，attrValue,quickShow等属性
     * @param entities ProductAttrValueEntity集合
     * @param spuId
     */
    @Override
    public void updateBatchAttrValue(List<ProductAttrValueEntity> entities, Long spuId) {
        //由于某些空的attrValue不会在ProductAttrValue表中有值，所以采用删除再整体插入方法
        this.remove(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
        entities = entities.stream().map(productAttrValueEntity -> {
            productAttrValueEntity.setSpuId(spuId);
            return productAttrValueEntity;
        }).collect(Collectors.toList());

        this.saveBatch(entities);
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<ProductAttrValueEntity> page = this.page(
                new Query<ProductAttrValueEntity>().getPage(params),
                new QueryWrapper<ProductAttrValueEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 保存spu的规格参数 pms_product_attr_value
     * @param spuId
     * @param baseAttrs 基础属性
     */
    @Override
    public void saveBaseAttrs(Long spuId, List<BaseAttrs> baseAttrs) {
        if(baseAttrs==null||baseAttrs.size()==0){
            return;
        }
        //根据attrIds查出attrs
        List<Long> attrIds = baseAttrs.stream().map((baseAttr)->{
            return baseAttr.getAttrId();
        }).collect(Collectors.toList());
        List<AttrEntity> attrList = attrService.listByIds(attrIds);
        //定义一个HashMap临时保存attrId和attrName
        HashMap<Long,String> tempHashMap = new HashMap<>();
        attrList.forEach((attr)->{
            tempHashMap.put(attr.getAttrId(),attr.getAttrName());
        });

        List<ProductAttrValueEntity> productAttrValueEntities = baseAttrs.stream().map(baseAttr -> {
            ProductAttrValueEntity productAttrValueEntity = new ProductAttrValueEntity();
            productAttrValueEntity.setSpuId(spuId);
            productAttrValueEntity.setAttrName(tempHashMap.get(baseAttr.getAttrId()));
            productAttrValueEntity.setAttrId(baseAttr.getAttrId());
            productAttrValueEntity.setAttrValue(baseAttr.getAttrValues());
            productAttrValueEntity.setQuickShow(baseAttr.getShowDesc());

            return productAttrValueEntity;
        }).collect(Collectors.toList());

        this.saveBatch(productAttrValueEntities);
    }

    /**
     * 根据spuId获取spu参数
     * @param spuId
     */
    @Override
    public List<ProductAttrValueEntity> listBySpuId(Long spuId) {

        return this.list(new QueryWrapper<ProductAttrValueEntity>().eq("spu_id",spuId));
    }

}